package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.pojo.dto.*;
import cn.tedu.csmall.product.pojo.entity.PmsBrand;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVo;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVo;
import cn.tedu.csmall.product.service.BrandService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BrandServiceImpl implements BrandService {
    @Autowired
    BrandMapper mapper;
    @Override
    public void insert(BrandInsertDto brandInsertDto) {
        int i = mapper.countByName(brandInsertDto.getName());
        if (i > 0){
            String message = "该品牌已存在";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(brandInsertDto,pmsBrand);
        int insert = mapper.insert(pmsBrand);
        if (insert != 1){
            String message = "添加失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
    }

    @Override
    public List<BrandListItemVo> list() {
        List<BrandListItemVo> list = mapper.list();
        return list;
    }

    @Override
    public void disable(Long id) {
        updateEnable(id,0);
    }

    @Override
    public void enable(Long id) {
        updateEnable(id,1);
    }

    @Override
    public void delete(Long id) {
        BrandStandardVo brandStandardVo = mapper.selectById(id);
        if (brandStandardVo == null) {
            String message = "该对象不存在";
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
        int i = mapper.deleteById(id);
        if (i != 1){
            String message = "删除不成功";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
    }

    @Override
    public void updateInfoById(Long id, CategoryUpdateDto categoryUpdateDTO) {
        BrandStandardVo brandStandardVo = mapper.selectById(id);
        if (brandStandardVo == null) {
            String message = "该品牌不存在";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        int i = mapper.countByName(categoryUpdateDTO.getName());
        if (i > 0){
            String message = "品牌名只能有一个";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(categoryUpdateDTO,pmsBrand);
        pmsBrand.setId(id);
        int update = mapper.update(pmsBrand);
        if (update != 1){
            String message = "修改失败";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    public void updateEnable(Long id,Integer enable){
        log.debug("id:{},enable:{}",id,enable);
        BrandStandardVo standardById = mapper.getStandardById(id);
        if (standardById.getEnable() == enable){
            return;
        }
        BrandUpdateEnableDto adminUpdateDto = new BrandUpdateEnableDto();
        adminUpdateDto.setEnable(enable);
        adminUpdateDto.setId(id);
        mapper.updateEnableBuId(adminUpdateDto);
    }
}
